package com.college.resource.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.college.common.pojo.ao.Evaluate;
import com.college.common.pojo.dto.resource.AddEvaluateDTO;
import com.college.common.pojo.dto.resource.QueryEvaluateDTO;
import com.college.common.pojo.dto.resource.UpdateEvaluateDTO;
import com.college.common.pojo.model.page.PageParamRequest;
import com.college.common.response.CommonResult;
import com.college.common.utils.object.ObjectUtil;
import com.college.resource.mapper.EvaluateMapper;
import com.college.resource.service.EvaluateService;
import com.github.pagehelper.page.PageMethod;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商品评价 服务实现类
 * @author 小C工作室
 * @since 2025-02-22
 */
@Service
public class EvaluateServiceImpl extends ServiceImpl<EvaluateMapper, Evaluate> implements EvaluateService {
    @Autowired
    private EvaluateMapper evaluateMapper;

    @Override
    public List<Evaluate> queryList(QueryEvaluateDTO queryEvaluateDTO) {
        return evaluateMapper.queryList(queryEvaluateDTO);
    }

    @Override
    public List<Evaluate> queryListPage(QueryEvaluateDTO queryEvaluateDTO, PageParamRequest pageParamRequest) {
        PageMethod.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        return evaluateMapper.queryList(queryEvaluateDTO);
    }

    @Override
    public CommonResult<String> addEvaluate(AddEvaluateDTO addEvaluateDTO) {
        String evaluateContent = addEvaluateDTO.getEvaluateContent();
        if (StringUtils.isNotBlank(evaluateContent) && evaluateContent.length() > 200){
            return CommonResult.failed("商品评价最多可写200个字！");
        }
        return this.save(addEvaluateDTO)? CommonResult.success("商品评价新增成功") : CommonResult.failed("商品评价新增失败");
    }

    @Override
    public CommonResult<String> updateEvaluate(UpdateEvaluateDTO updateEvaluateDTO) {
        Evaluate evaluate = ObjectUtil.objectCopyProperties(updateEvaluateDTO, Evaluate.class);
        return this.updateById(evaluate) ? CommonResult.success("商品评价修改成功") : CommonResult.failed("商品评价修改失败");
    }
}
